package org.lyq.erp.config.exception.exceptionhandler;

import javax.servlet.http.HttpServletRequest;

import org.lyq.erp.config.exception.myexception.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * 全局异常处理器
 * @author admin
 * 1.首先创建一个class类
 * 2.必须加上注解@ControllerAdvice
 * 3.如果返回的错误结果是字符串，要加上注解@ResponseBody
 * 
 */
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public String defaultExceptionHandler(HttpServletRequest request,Exception e) {
    if(e instanceof MyException) {
    	e.printStackTrace();
    	return e.getMessage();
    }else {
    	e.printStackTrace();
    	return "访问服务器失败";
    }
}
}
